/**
 * Description:
 * Class Name:ThreadLocalTest
 * Date:2022/5/8 18:39
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第16章_多线程._16_9_线程相关类._16_9_1_ThreadLocal类;

public class ThreadLocalTest extends Thread{
    private ThreadLocalAccount account;
    public ThreadLocalTest(ThreadLocalAccount account, String name){
        super(name);
        this.account = account;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            if (i == 6){
                // 将账户名替换成当前线程名
                account.setName(getName());
            }
            System.out.println(account.getName() + " 账户i的值：" + i);
        }
    }

    public static void main(String[] args) {
        // 启动两个线程，两个线程共享同一个Account
        var at = new ThreadLocalAccount("初始名");
        new ThreadLocalTest(at, "线程甲").start();
        new ThreadLocalTest(at, "线程乙").start();
    }
}
